﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Akso.Authorization;
using Akso.Entities;
using Akso.ItemDetails.Dto;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Akso.ItemDetails
{
    [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemDetails)]
    public class ItemDetailAppService: AbpZeroTemplateAppServiceBase, IItemDetailAppService
    {
        private readonly IRepository<ItemDetail, Guid> _itemDetailsRepository;
        public ItemDetailAppService(IRepository<ItemDetail, Guid> itemDetailsRepository)
        {
            _itemDetailsRepository = itemDetailsRepository;
        }
        #region 查询数据
        private IQueryable<ItemDetail> getQuery(GetItemDetailsInput input)
        {
            var query = _itemDetailsRepository.GetAll()
                .WhereIf(
                    !input.Filter.IsNullOrWhiteSpace(),
                    t =>
                        t.ItemCategoryId.ToString().Contains(input.Filter)
                );
            return query;
        }
        public async Task<ListResultDto<ItemDetailListDto>> GetObjects(GetItemDetailsInput input)
        {
            var query = getQuery(input);
            var dataCount = await query.CountAsync();
            var data = await query.OrderBy(x => x.CreationTime).PageBy(input).ToListAsync();

            return new PagedResultDto<ItemDetailListDto>(
                dataCount,
                data.MapTo<List<ItemDetailListDto>>()
                );
        }
        #endregion

        #region 新增、编辑对象，带权限检查
        /// <summary>
        /// 新增、编辑对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemDetails_Create, AppPermissions.Pages_PSSModule_ItemDetails_Edit)]
        public async Task<ItemDetail> CreateOrUpdateObject(CreateOrUpdateItemDetailInput input)
        {
            if (input.ItemDetail.Id.HasValue)
            {
                return await UpdateObject(input);
            }
            else
            {
                return await CreateObject(input);
            }
        }

        /// <summary>
        /// 新增对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemDetails_Create)]
        protected virtual async Task<ItemDetail> CreateObject(CreateOrUpdateItemDetailInput input)
        {
            var o = input.ItemDetail.MapTo<ItemDetail>();
            
            return await _itemDetailsRepository.InsertAsync(o);
        }
        /// <summary>
        /// 编辑对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemDetails_Edit)]
        protected virtual async Task<ItemDetail> UpdateObject(CreateOrUpdateItemDetailInput input)
        {
            Debug.Assert(input.ItemDetail.Id != null, "input.Classroom.Id should be set.");

            var o = await _itemDetailsRepository.GetAsync(input.ItemDetail.Id.Value);
            //Update classroom properties
            input.ItemDetail.MapTo(o);
            return await _itemDetailsRepository.UpdateAsync(o);
        }
        #endregion

        #region 删除数据，带权限检查
        /// <summary>
        /// 删除对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemDetails_Delete)]
        public async Task DeleteObject(EntityDto<Guid> input)
        {
            var o = await _itemDetailsRepository.GetAsync(input.Id);
            await _itemDetailsRepository.DeleteAsync(o);
        }
        #endregion

        #region 根据Guid获取数据对象
        /// <summary>
        /// 新增、编辑页面中获取对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_PSSModule_ItemDetails_Create, AppPermissions.Pages_PSSModule_ItemDetails_Edit)]
        public async Task<GetItemDetailForEditOutput> GetObjectForEdit(NullableIdDto<Guid> input)
        {
            var output = new GetItemDetailForEditOutput
            {

            };

            if (!input.Id.HasValue)
            {
                //Creating a new object
                output.ItemDetail = new ItemDetailEditDto();
            }
            else
            {
                //Editing an existing object
                var existingObject = await _itemDetailsRepository.GetAsync(input.Id.Value);

                output.ItemDetail = existingObject.MapTo<ItemDetailEditDto>();
            }

            return output;
        }
        #endregion
    }
}
